package org.example.servlet;

import org.example.dao.ChannelDao;
import org.example.model.Channel;
import org.example.model.ResponseStatus;
import org.example.util.Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/channel")
public class ChannelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        ResponseStatus response = new ResponseStatus();

        try{
            //查询所有频道列表返回
            List<Channel> list = ChannelDao.query();
            response.setOk(true);
            response.setData(list);  //list对象存储的就是频道信息
            //返回了ok：true  data：[{},{}]
        }catch(Exception e){
            e.printStackTrace();
            //后端报错返回空list,也就是说没有频道信息展示出来
            response.setReason(e.getMessage());
            //ok：false  reason：错误信息
        }
        resp.getWriter().println(Util.serialize(response));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
    }
}
